這次將從tensorflow的retrain model測試,首先先透過官方的flower資料集訓練
cd ~
curl -LO http://download.tensorflow.org/example_images/flower_photos.tgz
tar xzf flower_photos.tgz
mkdir ~/example_code
cd ~/example_code
curl -LO https://github.com/tensorflow/hub/raw/master/examples/image_retraining/retrain.py
python retrain.py --image_dir ~/flower_photos
訓練完成後,將開始測試此模型
curl -LO https://github.com/tensorflow/tensorflow/raw/master/tensorflow/examples/label_image/label_image.py
python label_image.py \
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--input_layer=Placeholder \
--output_layer=final_result \
--image=~/flower_photos/daisy/21652746_cc379e0eea_m.jpg
使用openvino測試模型,首先先將tensorflow訓練好的模型pb轉換成openvino使用的格式
cd /tmp
~/intel/openvino/deployment_tools/model_optimizer/mo_tf.py --input_model output_graph.pb --batch 1 --data_type FP16
基於openvino模型使用
from openvino.inference_engine import IENetwork, IEPlugin
import numpy as np
import cv2
import sys
import time
import os
def load_labels():
labels = []
with open('/tmp/output_labels.txt') as f:
for i in f.readlines():
labels.append(i.replace("\n", ""))
return labels
if __name__ == '__main__':
labels = load_labels()
plugin = IEPlugin(device="MYRIAD")
net = IENetwork(model="/tmp/output_graph.xml", weights="/tmp/output_graph.bin")
input_blob = next(iter(net.inputs))
out_blob = next(iter(net.outputs))
exec_net = plugin.load(network=net)
img = cv2.imread('~/flower_photos/daisy/21652746_cc379e0eea_m.jpg')
img = img[:,:,::-1]
img = cv2.resize(img, (299,299), cv2.INTER_CUBIC)
img = img/255.0
img = img.transpose((2, 0, 1))
img = img.reshape(1, 3, 299, 299)
req_handle = exec_net.start_async(request_id=0,inputs={input_blob:img})
status = req_handle.wait()
res = req_handle.outputs[out_blob]
res = np.squeeze(res)
top_inds = res.argsort()[-5:][::-1]
for i in top_inds:
print(labels[i], res[i])
辨識結果與tensorflow辨識結果相似